class Twitter {

    public static final AtomicInteger COUNTER = new AtomicInteger(0);

    static class Blog implements Comparable<Blog> {
        public int id;
        public int userId;
        public int priority;
        public Blog(int id, int userId, int priority) {
            this.id = id;
            this.userId = userId;
            this.priority = priority;
        }
        @Override
        public int compareTo(Blog other) {
            return other.priority - this.priority;
        }
        @Override
        public int hashCode() {
            return Objects.hashCode(this.id);
        }
        @Override
        public boolean equals(Object other) {
            return other instanceof Blog && this.id == ((Blog)other).id;
        }
    }
    /**
     * key: userId
     * data: 博文
     */
    Map<Integer, Set<Blog>> data;
    /**
     * key: userId
     * value: 关注key的用户
     */
    Map<Integer, Set<Integer>> followData;

    /** Initialize your data structure here. */
    public Twitter() {
        data = new HashMap<>();
        followData = new HashMap<>();
    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        Set<Blog> userData = data.getOrDefault(userId, new TreeSet<>());
        Blog tweet = new Blog(tweetId, userId, COUNTER.getAndIncrement());
        userData.add(tweet);
        data.put(userId, userData);
        Set<Integer> followers = followData.get(userId);
        if (followers != null) {
            for (Integer followerId : followers) {
                Set<Blog> followerData = data.getOrDefault(followerId, new TreeSet<>());
                followerData.add(tweet);
                data.put(followerId, followerData);
            }
        }
    }

    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> ans = new ArrayList<>();
        Set<Blog> userData = data.get(userId);
        if (userData == null || userData.size() == 0) {
            return ans;
        }
        int i = 0;
        for (Blog blog: userData) {
            if (i == 10) {
                break;
            }
            ans.add(blog.id);
            i++;
        }
        return ans;
    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        Set<Integer> followerOfFollowee = followData.getOrDefault(followeeId, new HashSet<>());
        followerOfFollowee.add(followerId);
        followData.put(followeeId, followerOfFollowee);
        Set<Blog> followeeData = data.get(followeeId);
        if (followeeData == null || followeeData.size() == 0) {
            return;
        }
        Set<Blog> followerData = data.getOrDefault(followerId, new TreeSet<>());
        followeeData.stream().filter(blog -> blog.userId == followeeId).forEach(followerData::add);
        data.put(followerId, followerData);
    }

    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        Set<Integer> followerOfFollowee = followData.get(followeeId);
        if (followerOfFollowee == null || !followerOfFollowee.contains(followerId)) {
            return;
        }
        Set<Blog> followerData = data.get(followerId);
        data.get(followeeId).stream().filter(blog -> blog.userId == followeeId).forEach(followerData::remove);
        data.put(followerId, followerData);
        followerOfFollowee.remove(followerId);
        followData.put(followeeId, followerOfFollowee);
    }

}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */

